package com.roncoo.education.course.service.api.pc.biz;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.roncoo.education.course.common.dto.AnswerCountDto;
import com.roncoo.education.course.common.dto.AnswerOptionCountDto;
import com.roncoo.education.course.common.dto.AnswerTotalDto;
import com.roncoo.education.course.common.dto.CountAnswerDto;
import com.roncoo.education.course.common.req.AnswerREQ;
import com.roncoo.education.course.service.dao.impl.mapper.entity.Topic;
import com.roncoo.education.course.service.dao.impl.mapper.entity.TopicAnswer;
import com.roncoo.education.course.service.dao.service.impl.mapper.TopicAnswerMapper;
import com.roncoo.education.course.service.dao.service.impl.mapper.TopicMapper;
import com.roncoo.education.util.base.Result;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * 试卷题目
 */
@Component
public class PcApiTopIcAnswerBiz {

    @Resource
    private TopicAnswerMapper mapper;

    @Resource
    private TopicMapper topicMapper;



    public Result<Page> findPageList(AnswerREQ answerREQ){
        Page<AnswerTotalDto> page=new Page<>(answerREQ.getPageCurrent(),answerREQ.getPageSize());
        List<AnswerTotalDto> list=mapper.getTotalPage(page);
        return Result.success(page.setRecords(list));
    }

    public Result<Page> countAnswer(AnswerREQ answerREQ){
        Page<CountAnswerDto> page=new Page<>(answerREQ.getPageCurrent(),answerREQ.getPageSize());
        List<CountAnswerDto> list=topicMapper.getPageList(answerREQ,page);
        //循环试卷下所有题目
        list.forEach(t->{
            //将题目转换成list 方便计算用户人数
            List<String>topics=JSONArray.parseArray(t.getTopic(),String.class);
            List<AnswerCountDto> answers=new ArrayList<>();
            topics.forEach(a->{
                AnswerCountDto answerCountDto=new AnswerCountDto();
                answerCountDto.setTopic(a);
                answers.add(answerCountDto);
            });
            //根据题目id 获取每个选项用户人数
            List<AnswerOptionCountDto> options=mapper.countOptions(t.getId());
            options.forEach(b->{
                //根据题目选项 把
                answers.get(b.getAnswer()).setNum(b.getNum());
            });
            t.setTopics(answers);
        });

        return Result.success(page.setRecords(list));

    }


}
